var express = require('express');
var router = express.Router();
// sql
const query = require('./mysql');
// encrypt
let encrypt = require('./util/encrypt');
// register_time
let getTime = require('./util/getTime');
router.post('/', async (req, res) => {
	let user = req.body.user;
	console.log(req.body.user);
	let { username, account, password, college, position, admin } = user;

	let sqlStr = 'select * from user where username = ?';
	let result = await query(sqlStr, [user.username]);
	if (result.length != 0) {
		res.send({ status: 400, message: '用户名已注册' });
	} else {
		let sqlStr =
			'insert into user(username,account,password,college,position,admin,register_time,isLogin) values (?,?,?,?,?,?,?,?)';
		let result = await query(sqlStr, [
			username,
			account,
			encrypt(password),
			college,
			position,
			admin,
			getTime(),
			0,
		]);
		if (result.length != 0) {
			res.send({ status: 200, message: '注册成功' });
		} else {
			res.send({ status: 404, message: '注册失败' });
		}
	}
});

module.exports = router;
